package top.dashuizhuyu.mdx;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.Base64.Encoder;

import javax.net.ssl.HttpsURLConnection;

public class Image {
	

	public static String pathAbsolute(String filePath,String imgPath){
		if(imgPath.toLowerCase().startsWith("http")){
			return imgPath;
		}
		
		File file = new File(imgPath);
		if(file.isAbsolute()){
			return imgPath;
		}else{
			if(!imgPath.startsWith("../")){
				imgPath = "../" + imgPath; 
			}
			File imgFile = new File(filePath,imgPath);
			try {
				return imgFile.getCanonicalPath();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "";
	}
	
	/**
	 * 图片转BASE64编码
	 * 
	 * @param absolutePath 只接受一个绝对路径，无论网络还是本地
	 * @return BASE64字符串
	 */
	public static String image2Base64(String absolutePath) {
		
		if(absolutePath == null || "".equals(absolutePath) || (!new File(absolutePath).isAbsolute() && !absolutePath.toLowerCase().startsWith("http") )){
			return "";
		}
	   
		// D:\\
		// http:  https:
		try{
			if(absolutePath.toLowerCase().startsWith("http")){
				return webImage2Base64(absolutePath);
			}else{
				return localImage2Base64(absolutePath);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return "";
		
		
	}
	
	
	
	/**
	 * 本地图片转BASE64字符串
	 * 
	 * @param imgPath 图片路径
	 * @return Base64字符串
	 */
	private static String localImage2Base64(String imgPath) {
	    byte[] data = null;
	    // 读取图片字节数组
	    try {
	        InputStream in = new FileInputStream(imgPath);
	        data = new byte[in.available()];
	        in.read(data);
	        in.close();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    // 对字节数组Base64编码
	    Encoder encoder = Base64.getEncoder();
	    // 返回Base64编码过的字节数组字符串
	    return encoder.encodeToString(data);
	}
	
	/**
	 * 网络图片转BASE64编码
	 * 
	 * @param imgURL 图片地址
	 * @return Base64字符串
	 */
	private static String webImage2Base64(String imgURL) {
	    ByteArrayOutputStream outPut = new ByteArrayOutputStream();
	    byte[] data = new byte[1024];
	    try {
	        // 创建URL
	        URL url = new URL(imgURL);
	        // 创建链接
	        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
	        conn.setRequestMethod("GET");
	        conn.setConnectTimeout(10 * 1000);

	        //连接失败/链接失效/图片不存在
	        if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
	            return "";
	        }
	        InputStream inStream = conn.getInputStream();
	        int len = -1;
	        while ((len = inStream.read(data)) != -1) {
	            outPut.write(data, 0, len);
	        }
	        inStream.close();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    // 对字节数组Base64编码
	    Encoder encoder = Base64.getEncoder();
	    // 返回Base64编码过的字节数组字符串
	    return encoder.encodeToString(outPut.toByteArray());
	}
	

}
